import {
  ActionType,
  ModalForm,
  PageContainer,
  ProColumns,
  ProFormDigit,
  ProFormText,
  ProTable
} from "@ant-design/pro-components";
import React, {useRef, useState} from "react";
import {Button, Form} from "antd";
import {EditOutlined, PlusOutlined, SettingFilled} from "@ant-design/icons";
import {globalLoadTableApi, handleGloblApi} from "@/services/admin-api/globalApi";

type Authority = {
  authorityId: number;
  authorityPid: number;
  authorityName: string;
  authorityCode: string;
  delFlag: boolean;
  createTime: string;
  createUserId: number;
  createUserName: string;
  updateTime: string;
  updateUserId: number;
  updateUserName: string;
};

export default () => {

  const [modalOpen, handleModalOpen] = useState(false);
  const [form] = Form.useForm();
  const actionRef = useRef<ActionType>();
  // const [currentRow, setCurrentRow] = useState<ProductType>();

  const columns: ProColumns<Authority>[] = [
    // {title: '', valueType: 'indexBorder'},
    {title: 'authorityId', dataIndex: 'authorityId', valueType: 'digit'},
    {title: 'authorityPid', dataIndex: 'authorityPid', valueType: 'digit', hideInSearch: true,},
    {title: 'authorityName', dataIndex: 'authorityName', valueType: 'text'},
    {title: 'authorityCode', dataIndex: 'authorityCode', valueType: 'text',},
    {title: '创建时间', dataIndex: 'createTime', valueType: 'dateTime', hideInSearch: true,},
    {title: '创建用户ID', dataIndex: 'createUserId', valueType: 'digit', hideInSearch: true,},
    {title: '创建用户', dataIndex: 'createUserName', valueType: 'text', hideInSearch: true,},
    {title: '更新时间', dataIndex: 'updateTime', valueType: 'dateTime', hideInSearch: true,},
    {title: '更新用户ID', dataIndex: 'updateUserId', valueType: 'digit', hideInSearch: true,},
    {title: '更新用户', dataIndex: 'updateUserName', valueType: 'text', hideInSearch: true,},
    {
      title: <SettingFilled/>,
      render: (_, record) => [
        <Button
          key={'edit_' + record.authorityId} size="small" icon={<EditOutlined/>} type="primary"
          onClick={async () => {
            form.setFieldsValue({...record});
            handleModalOpen(true);
          }}
        />,
        <Button
          key={'add_' + record.authorityId} size="small" icon={<PlusOutlined/>} type="primary"
          onClick={async () => {
            form.setFieldsValue({authorityPid: record.authorityId});
            handleModalOpen(true);
          }}
        />,
      ],
      valueType: 'option',
    },
  ];

  return <PageContainer header={{title: '', breadcrumb: {},}}>
    <ProTable<Authority>
      columns={columns} actionRef={actionRef} pagination={{pageSizeOptions: [1000], pageSize: 1000}}
      search={{span: 4}}
      headerTitle={
        <Button
          key="primary" type="primary" icon={<PlusOutlined/>} onClick={() => {
          handleModalOpen(true)
        }}/>
      }
      request={async (param) => globalLoadTableApi('/sys/authorityTable', param)}
      rowKey={(record) => record.authorityId}
      columnsState={{
        defaultValue: {
          authorityPid: {show: false},
          delFlag: {show: false},
          createUserId: {show: false},
          updateUserId: {show: false}
        }
      }}/>

    <ModalForm
      modalProps={{destroyOnClose: true}} autoFocusFirstInput submitTimeout={15000}
      width="400px" open={modalOpen} onOpenChange={handleModalOpen} form={form}
      onFinish={async (value) => {
        console.log('value = ', value);
        let success = await handleGloblApi('/sys/authoritySaveOrUpdate', value)

        if (success) {
          actionRef.current?.reload();
          return true
        }

      }}
    >
      <ProFormDigit label="authorityId" name="authorityId" hidden/>
      <ProFormDigit label="authorityPid" name="authorityPid" hidden initialValue={0}/>
      <ProFormText label="authorityName" rules={[{required: true}]} name="authorityName"/>
      <ProFormText label="authorityCode" rules={[{required: true}]} name="authorityCode"/>

    </ModalForm>

  </PageContainer>
}
